//
//  CZVisitorView.swift
//  weibo
//
//  Created by 丁蒙 on 16/6/25.
//  Copyright © 2016年 丁蒙. All rights reserved.
//

import UIKit
import SnapKit

class CZVisitorView: UIView {
    //点击登录/注册按钮 闭包
    var compose:(()->())?
    
    // MARK - 添加动画
    func setAnimation() {
        
        let basicAni = CABasicAnimation(keyPath: "transform.rotation")
        basicAni.toValue = M_PI * 2
        basicAni.repeatCount = MAXFLOAT
        basicAni.duration = 10
        //进入后台不停止
        basicAni.removedOnCompletion = false
        
        circleImageView.layer.addAnimation(basicAni, forKey: nil)
        
    }

    
    // MARK - 添加子控件
    override init(frame: CGRect) {
        super.init(frame: frame)
        addSubview(circleImageView)
        addSubview(iconImageView)
        addSubview(maskImageView)
        addSubview(titleLabel)
        addSubview(loginButton)
        addSubview(registerButton)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //设置约束
    override func layoutSubviews() {
        circleImageView.snp_makeConstraints { (make) in
            make.center.equalTo(self)
        }
        iconImageView.snp_makeConstraints { (make) in
            make.center.equalTo(self)
        }
        maskImageView.snp_makeConstraints { (make) in
            make.center.equalTo(self)
        }
        titleLabel.snp_makeConstraints { (make) in
            make.top.equalTo(maskImageView.snp_bottom).offset(16)
            make.width.equalTo(224)
            make.height.equalTo(36)
            make.centerX.equalTo(self)
        }
        loginButton.snp_makeConstraints { (make) in
            make.left.equalTo(titleLabel)
            make.top.equalTo(titleLabel.snp_bottom).offset(16)
            make.size.equalTo(CGSize(width: 100, height: 36))
        }
        registerButton.snp_makeConstraints { (make) in
            make.right.equalTo(titleLabel)
            make.top.equalTo(titleLabel.snp_bottom).offset(16)
            make.size.equalTo(CGSize(width: 100, height: 36))
        }
    }
    
    func setUpVisitorViewInfo(imgName: String?,title: String?) {
        
        if let img = imgName,let tit = title {//不是首页
            
            iconImageView.image = UIImage(named: img)
            titleLabel.text = tit
            
            circleImageView.hidden = true
        } else {//首页
            setAnimation()
        }
    }
    //圆圈
    private lazy var circleImageView: UIImageView = {
       
        let circle = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))

        return circle
        
    }()
    //中间视图
    private lazy var iconImageView: UIImageView = {
        let icon = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
        return icon
    }()
    //蒙版视图
    private lazy var maskImageView: UIImageView = {
        let mask = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
        return mask
    }()
    //文字Label
    private lazy var titleLabel: UILabel = {
        let titleLB = UILabel()
        titleLB.text = "关注一些人，回这里看看有什么惊喜关注一些人，回这里看看有什么惊喜"
        titleLB.textColor = ThemeColor
        titleLB.font = UIFont.systemFontOfSize(14)
        titleLB.numberOfLines = 0
        titleLB.textAlignment = .Center
        
        return titleLB
        
    }()
    //登录
    private lazy var loginButton: UIButton = {
       let loginBtn = UIButton()
        loginBtn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        loginBtn.setTitle("登录", forState: UIControlState.Normal)
        loginBtn.setTitleColor(ThemeColor, forState: UIControlState.Normal)
        loginBtn.addTarget(self, action: #selector(CZVisitorView.buttonClick), forControlEvents: UIControlEvents.TouchUpInside)
        
        return loginBtn
    }()
    //注册
    private lazy var registerButton: UIButton = {
        let registerBtn = UIButton()
        registerBtn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        registerBtn.setTitle("注册", forState: UIControlState.Normal)
        registerBtn.setTitleColor(ThemeColor, forState: UIControlState.Normal)
        registerBtn.addTarget(self, action: #selector(CZVisitorView.buttonClick), forControlEvents: UIControlEvents.TouchUpInside)
        
        return registerBtn

    }()
    
    @objc private func buttonClick() {
        compose?()
    }
    
}
